package com.fram.controller.admin;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseAdminController;
import com.fram.entity.DistributorFarm;
import com.fram.entity.ProjectGoods;
import com.fram.entity.ProjectGoodsPrice;
import com.fram.service.admin.CategroyService;
import com.fram.service.admin.DistributorService;
import com.fram.service.admin.GoodsService;

/**
 * @作者：大晓刚 @时间 2020年5月19日 @dascription: ->后台-产品管理
 */
@RestController
@RequestMapping("/admin/{version}/auth/goods")
public class GoodsController extends BaseAdminController {

	/** 日志 **/
	private static Logger LOG = LoggerFactory.getLogger(GoodsController.class);

	@Autowired
	private GoodsService goods;
	@Autowired
	private CategroyService categroyService;
	@Autowired
	private DistributorService distributorService;
	
	//增加商品需要展示的 分类  
	@ApiVersion(1.0)
	@RequestMapping("listWithChildren")
	private ResponseMsg listWithChildren(Long distributorId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if(distributorId == null) {
				msg.setData(categroyService.listWithChildren(getDistributorId()));
			}else {
				msg.setData(categroyService.listWithChildren(distributorId));
			}
		}catch (ProException e) {
			LOG.error("-针对店铺-商品分类下拉出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-针对店铺-商品分类下拉异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("withchildren")
	private ResponseMsg WithChildren(@RequestBody PageBean<DistributorFarm> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.getParam().setId(getDistributorId());
			msg.setData(distributorService.withChildren(request));
		}catch (ProException e) {
			LOG.error("-店铺下拉出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-店铺下拉异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	/**
	 * 查询所有商品 根据名称模糊查询
	 * @throws ProException
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	private ResponseMsg list(@RequestBody PageBean<ProjectGoods> request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getParam() == null) {
				request.setParam(new ProjectGoods());
			}
			if (getAdminParamUserId() != null) {
				request.getParam().setDistributorId(getDistributorId());
			}
			msg.setData(goods.getProjectGoodsList(request));
		} catch (ProException e) {
			LOG.error("-查询所有商品出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-查询所有商品异常-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 增加商品
	 * @param request
	 * @throws ProException
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	private ResponseMsg add(@RequestBody ProjectGoods request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if(request.getDistributorId() == null) {
				request.setDistributorId(getDistributorId());
			}
			msg.setData(goods.insertProjectGoods(request));
		} catch (ProException e) {
			LOG.error("-增加商品->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-增加商品--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 根据主键id 查询详情内容进行修改
	 * 
	 * @param id
	 * @return
	 * @throws ProException
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/details", method = RequestMethod.GET)
	private ResponseMsg details(Long id) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goods.selectById(id));
		} catch (ProException e) {
			LOG.error("-根据主键id查询商品->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-根据主键id查询删除商品--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 查询详情内容进行修改
	 * 
	 * @param request
	 * @return
	 * @throws ProException
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	private ResponseMsg update(@RequestBody ProjectGoods request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goods.updateProjectGoods(request);
		} catch (ProException e) {
			LOG.error("-修改商品->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-修改商品--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/****/
	@ApiVersion(1.0)
	@RequestMapping(value = "price/list")
	private ResponseMsg priceList(@RequestBody PageBean<ProjectGoodsPrice> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goods.listPrice(request));
		} catch (ProException e) {
			LOG.error("-价格列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-价格列表异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "price/add")
	private ResponseMsg priceAdd(@RequestBody ProjectGoodsPrice request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goods.addPrice(request);
		} catch (ProException e) {
			LOG.error("-价格增加出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-价格增加异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "price/update")
	private ResponseMsg priceUpdate(@RequestBody ProjectGoodsPrice request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goods.updatePrice(request);
		} catch (ProException e) {
			LOG.error("-价格修改出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-价格修改异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "price/update/list")
	private ResponseMsg priceUpdateList(@RequestBody List<ProjectGoodsPrice> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goods.updatePriceList(request);
		} catch (ProException e) {
			LOG.error("-价格修改出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-价格修改异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "price/details", method = RequestMethod.GET)
	private ResponseMsg priceDetails(Long id) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goods.detailsPrice(id));
		} catch (ProException e) {
			LOG.error("-价格详情出错->{},{}", e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-价格详情出错--->{}", PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 后台下拉 分类展示
	 * 
	 * @return
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/listcategroy", method = RequestMethod.GET)
	private ResponseMsg listCategroy() throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(categroyService.listCategory(getDistributorId()));
		} catch (ProException e) {
			LOG.error("-下拉分类展示->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-下拉分类展示--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 退货原因 **/
	// 增加
	// 修改
	// 删除
	// 查询
	// 根据id
}
